/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.editor.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.rmi.server.UID;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

import com.genia.toolbox.basics.editor.AbstractController;
import com.genia.toolbox.basics.editor.message.AbstractMessages;
import com.genia.toolbox.basics.editor.model.bean.InitialSettings;
import com.genia.toolbox.basics.editor.model.document.Document;
import com.genia.toolbox.basics.editor.model.document.Element;

/**
 * Define an abstract toolbar.
 * 
 * @param <G>
 *          The generic GUI.
 * @param <C>
 *          The generic controller.
 * @param <D>
 *          The generic document.
 * @param <E>
 *          The generic element.
 * @param <I>
 *          The generic initial settings
 */
@SuppressWarnings("serial")
public abstract class AbstractToolBar<G extends AbstractGUI<?, ?, C, D, E, I>, C extends AbstractController<?, G, D, E, I>, D extends Document<E>, E extends Element<?>, I extends InitialSettings>
    extends JToolBar
    implements ActionListener
{

  /**
   * The gui.
   */
  private G gui;

  /**
   * The open button.
   */
  private JButton buttonOpen;

  /**
   * The create button.
   */
  private JButton buttonCreate;

  /**
   * The cut button.
   */
  private JButton buttonCut;

  /**
   * The paste button.
   */
  private JButton buttonPaste;

  /**
   * The save button.
   */
  private JButton buttonSave;

  /**
   * The save all button.
   */
  private JButton buttonSaveAll;



  /**
   * Constructor.
   * 
   * @param gui
   *          the gui.
   */
  public AbstractToolBar(G gui)
  {
    super();
    this.gui = gui;
    this.setFloatable(false);
    this.setRollover(true);
    this.setOrientation(SwingConstants.HORIZONTAL);
  }



  /**
   * Initialise the common commands of the tool bar.
   */
  public void initialise()
  {
    URL url = AbstractToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/create.gif");
    this.buttonCreate = new JButton(new ImageIcon(url));
    this.buttonCreate.addActionListener(this);
    this.buttonCreate.setToolTipText(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_CREATE));
    this.add(this.buttonCreate);

    url = AbstractToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/open.gif");
    this.buttonOpen = new JButton(new ImageIcon(url));
    this.buttonOpen.addActionListener(this);
    this.buttonOpen.setToolTipText(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_OPEN));
    this.add(this.buttonOpen);

    this.addSeparator(new Dimension(AbstractGUI.TOOLBAR_SEPARATOR_WIDTH, 16));

    url = AbstractToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/cut.gif");
    this.buttonCut = new JButton(new ImageIcon(url));
    this.buttonCut.addActionListener(this);
    this.buttonCut.setToolTipText(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_CUT));
    this.add(this.buttonCut);

    url = AbstractToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/paste.gif");
    this.buttonPaste = new JButton(new ImageIcon(url));
    this.buttonPaste.addActionListener(this);
    this.buttonPaste.setToolTipText(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_PASTE));
    this.add(this.buttonPaste);

    this.addSeparator(new Dimension(AbstractGUI.TOOLBAR_SEPARATOR_WIDTH, 16));

    url = AbstractToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/save.gif");
    this.buttonSave = new JButton(new ImageIcon(url));
    this.buttonSave.addActionListener(this);
    this.buttonSave.setToolTipText(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_SAVE));
    this.add(this.buttonSave);

    url = AbstractToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/saveall.gif");
    this.buttonSaveAll = new JButton(new ImageIcon(url));
    this.buttonSaveAll.addActionListener(this);
    this.buttonSaveAll.setToolTipText(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_SAVEALL));
    this.add(this.buttonSaveAll);

    this.addSeparator(new Dimension(AbstractGUI.TOOLBAR_SEPARATOR_WIDTH, 16));

    this.initialiseSpecificCommands();
  }



  /**
   * Initialise the specific commands of the tool bar.
   */
  public abstract void initialiseSpecificCommands();



  /**
   * Refresh the access of the items.
   */
  public void refresh()
  {
    this.buttonCreate.setEnabled(true);
    this.buttonOpen.setEnabled(true);
    Document<E> template = this.getGui().getSelectedDocument();
    if (template == null) {
      this.buttonSave.setEnabled(false);
      this.buttonCut.setEnabled(false);
      this.buttonPaste.setEnabled(false);
    }
    else {
      this.buttonSave.setEnabled(true);
      Element<?> element = this.getGui().getSelectedElement();
      if (element != null && element.getParentID() != null) {
        this.buttonCut.setEnabled(true);
      }
      else {
        this.buttonCut.setEnabled(false);
      }
      if (this.getGui().getController().notifyHasCutElement()) {
        if (element != null && element.isContainer()) {
          this.buttonPaste.setEnabled(true);
        }
        else {
          this.buttonPaste.setEnabled(false);
        }
      }
      else {
        this.buttonPaste.setEnabled(false);
      }
    }
    List<UID> templates = this.getGui().getAllDocumentsUniqueID();
    if (templates.isEmpty()) {
      this.buttonSaveAll.setEnabled(false);
    }
    else {
      this.buttonSaveAll.setEnabled(true);
    }
  }



  /**
   * Handle the action events.
   * 
   * @param e
   *          the action event.
   */
  public void actionPerformed(ActionEvent e)
  {
    Object source = e.getSource();
    if (source.equals(this.buttonOpen)) {
      this.getGui().getController().notifyCreateOpenDocument();
    }
    else if (source.equals(this.buttonCreate)) {
      I settings = this.getGui().createElement();
      this.getGui().getController().notifyCreateNewDocument(settings);
    }
    else if (source.equals(this.buttonSave)) {
      D document = this.getGui().getSelectedDocument();
      this.getGui().getController().notifySaveDocument(document);
    }
    else if (source.equals(this.buttonSaveAll)) {
      this.getGui().getController().notifySaveAllDocuments();
    }
    else if (source.equals(this.buttonCut)) {
      E element = this.getGui().getSelectedElement();
      this.getGui().getController().notifyCutElement(element);
    }
    else if (source.equals(this.buttonPaste)) {
      E element = this.getGui().getSelectedElement();
      this.getGui().getController().notifyPasteElement(element);
    }
  }



  /**
   * Get the gui.
   * 
   * @return the gui.
   */
  public G getGui()
  {
    return this.gui;
  }



  /**
   * Get the cut button.
   * 
   * @return the cut button.
   */
  public JButton getButtonCut()
  {
    return buttonCut;
  }



  /**
   * Get the paste button.
   * 
   * @return the paste button.
   */
  public JButton getButtonPaste()
  {
    return buttonPaste;
  }

}
